﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;

namespace Web.Framework.Svc
{
    public static class AuditoryHelper
    {
        public static IQueryable<T> FilterLogicalRemoved<T>(this IQueryable<T> query)
        {
            Type typeOfT = typeof(T);
            PropertyInfo propertyInfo = typeOfT.GetProperty("Removed");
            if (propertyInfo != null)
            {
                ParameterExpression param = Expression.Parameter(typeof(T), "x"); // x
                Expression property = Expression.Property(param, propertyInfo); //x.ColumnName              
                var lambda = Expression.Lambda<Func<T, bool>>(Expression.NotEqual(Expression.Convert(property, typeof(bool)), Expression.Constant(true)), param);
                query = query.Where(lambda);
            }

            return query;
        }        
    }
}
